#ifndef _H_COMMONPOOL_
#define _H_COMMONPOOL_

#include <stdio.h>

#define COMMONPOOL_ERROR_SUCCESS			0
#define COMMONPOOL_ERROR_INTERNAL			1
#define COMMONPOOL_ERROR_ALLOC				11
#define COMMONPOOL_ERROR_POOL_STATUS			101
#define COMMONPOOL_ERROR_CREATE_WATCHING_THREAD		102
#define COMMONPOOL_ERROR_POOL_STOPING			103
#define COMMONPOOL_ERROR_MUTEX				104
#define COMMONPOOL_ERROR_TOO_MANY_SESSIONS		105
#define COMMONPOOL_ERROR_NO_IDLE_SESSION		106
#define COMMONPOOL_ERROR_CREATE_SESSION			1001
#define COMMONPOOL_ERROR_WATCHING_SESSION		1002

struct CommonPool ;

typedef int funcInitSessionContext( struct CommonPool *pool , void *session_ctx );
typedef int funcWatchingSessions( struct CommonPool *pool );
typedef int funcCleanSessionContext( struct CommonPool *pool , void *session_ctx );

struct CommonPoolCallback
{
	size_t			session_ctx_size ;
	
	funcInitSessionContext	*pfuncInitSessionContext ;
	funcWatchingSessions	*pfuncWatchingSessions ;
	funcCleanSessionContext	*pfuncCleanSessionContext ;
} ;

struct CommonPool *CreateCommonPool( struct CommonPoolCallback *callback , size_t comm_pool_callback_size );
int StartCommonPool( struct CommonPool *pool );
int StopCommonPool( struct CommonPool *pool );
int DestroyCommonPool( struct CommonPool *pool );

int EnableAssistantThread( struct CommonPool *pool , unsigned char assistant_thread_enable );
int SetCommonPoolMinIdleSessionsCount( struct CommonPool *pool , size_t min_idle_sessions_count );
int SetCommonPoolMaxIdleSessionsCount( struct CommonPool *pool , size_t max_idle_sessions_count );
int SetCommonPoolMaxSessionsCount( struct CommonPool *pool , size_t max_sessions_count );
int SetCommonPoolMaxIdleTimeval( struct CommonPool *pool , size_t max_idle_timeval );
int SetCommonPoolWatchIdleTimeval( struct CommonPool *pool , size_t max_idle_timeval );
int SetCommonPoolInspectTimeval( struct CommonPool *pool , size_t inspect_timeval );
void SetCommonPoolUserData( struct CommonPool *pool , void *user_data );
void *GetCommonPoolUserData( struct CommonPool *pool );

int FetchCommonPoolSession( struct CommonPool *pool , void **session_ctx );
int JustFetchCommonPoolIdleSession( struct CommonPool *pool , void **session_ctx );
int GivebackCommonPoolSession( struct CommonPool *pool , void *session_ctx );

int ReleaseCommonPoolIdleSession( struct CommonPool *pool , void *session_ctx );

size_t GetCommonPoolIdleSessionsCount( struct CommonPool *pool );
size_t GetCommonPoolWorkingSessionsCount( struct CommonPool *pool );

/*
void *TravelCommonPoolIdleSession(struct CommonPool *pool , void *session_ctx , unsigned char change_status_idle2working );
void ChangebackAfterTravelCommonPoolSession( struct CommonPool *pool , void *session_ctx );
*/

#endif

